EAN5.js ➔ ... ➔ ???   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
// Encoding documentation:
2
// https://en.wikipedia.org/wiki/EAN_5#Encoding
3
4
import { EAN5_STRUCTURE } from './constants';
5
import encode from './encoder';
6
import Barcode from '../Barcode';
7
8
const checksum = (data) => {
9
	const result = data
10
		.split('')
11
		.map(n => +n)
12
		.reduce((sum, a, idx) => {
13
			return idx % 2
14
				? sum + a * 9
15
				: sum + a * 3;
16
		}, 0);
17
	return result % 10;
18
};
19
20
class EAN5 extends Barcode {
21
22
	constructor(data, options) {
23
		super(data, options);
24
	}
25
26
	valid() {
27
		return this.data.search(/^[0-9]{5}$/) !== -1;
28
	}
29
30
	encode() {
31
		const structure = EAN5_STRUCTURE[checksum(this.data)];
32
		return {
33
			data: '1011' + encode(this.data, structure, '01'),
34
			text: this.text
35
		};
36
	}
37
38
}
39
40
export default EAN5;
41